package com.eastedu.superise.handler.question.handler.completion;

import com.eastedu.utils.CollectionUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 内容分析概要信息
 *
 * @author luozhenzhong
 */
@Data
public class ContentSummary {

    /**
     * 无序的
     */
    private final boolean disordered;
    /**
     * 从1开始
     */
    private final boolean beginFrom1;
    /**
     * 序号连续不断胡
     */
    private final boolean continuous;
    /**
     * 序号数量
     */
    private final int orderCount;
    /**
     * 空格数量
     */
    private final int spaceCount;

    /**
     * 序号后面是否存在空格
     */
    private final boolean spaceMissing;
    private final Map<Optional<String>, List<String>> details;

    /**
     * 暂时用来处理无序号时，记录答案的段落数量
     */
    @Deprecated
    private int paragraphCount;

    /**
     * Instantiates a new Profile.
     *
     * @param details the details
     */
    public ContentSummary(Map<Optional<String>, List<String>> details) {
        this.details = details;

        if (CollectionUtils.isEmpty(this.details)) {
            this.disordered = true;
            this.continuous = false;
            this.beginFrom1 = false;
            this.orderCount = 0;
            this.spaceCount = 0;
            this.spaceMissing = false;
        } else {
            Set<Optional<String>> optionals = this.details.keySet();
            List<Integer> orders = optionals.parallelStream()
                    .filter(Optional::isPresent)
                    .map(Optional::get)
                    .map(s -> s.replaceAll("[()（）]", ""))
                    .map(s -> StringUtils.isBlank(s) ? "0" : s)
                    .map(Integer::parseInt)
                    .sorted()
                    .collect(Collectors.toList());

            this.orderCount = orders.size();
            // 计算有对应序号的空格总数
            if (this.orderCount == 0) {
                this.disordered = true;
                this.continuous = false;
                this.beginFrom1 = false;
                // 无序号时， 计算所有的空格数量
                this.spaceCount = this.details.entrySet().parallelStream().map(Map.Entry::getValue).map(List::size).reduce(0, Integer::sum);
                this.spaceMissing = this.spaceCount == 0;
            } else {
                this.disordered = false;
                this.continuous = isContinuous(orders);
                this.beginFrom1 = orders.stream().skip(0).limit(1).allMatch(o -> o == 1);
                // 有序号时，需要排除无序号的空格（无序号空格当作普通内容处理，因此不参与计算）
                this.spaceCount = this.details.entrySet().parallelStream().filter(entry -> entry.getKey().isPresent()).map(Map.Entry::getValue).map(List::size).reduce(0, Integer::sum);
                // 是否存在对应序号的空格数量为空的情况
                this.spaceMissing = this.details.values().parallelStream().anyMatch(List::isEmpty);
            }
        }
    }

    private boolean isContinuous(List<Integer> orders) {
        boolean continuous = true;
        for (int i = 0; i < orders.size(); i++) {
            Integer order = orders.get(i);
            if (!order.equals(i + 1)) {
                continuous = false;
                break;
            }
        }
        return continuous;
    }
}
